# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from dzcontrol import *
from dzapp import *

class PassReset(App):

    def __init__(self):
        # Attributes set in the constructor should independent of state
        import dzapp
        dzapp.App.__init__(self,'passreset')
        self.msg = ''
        self.err = 0
        self.title = 'Reset Password'

    def _view_(self):
        content = (Wiki(include('passresetfm.htm') % (self.msg,zoomer.sid)).render())
        return Page('Reset Password','Reset your password',content)


    def _change(self):
        reg_user     = zoomer.session.reg_user
        reg_email    = zoomer.session.reg_email
        zoomer.debug('user '+str(reg_user))
        if reg_user:
            content = Wiki(include('newpassfm.htm') % (self.msg,zoomer.sid)).render()
            return Page('Reset Password','Reset your password',content)
        else:
            c = Wiki(include('expired.txt')).render()
            page = Page('Reset Password')
            page.content = c
            return page

    def _commit(self):
        reg_user     = zoomer.session.reg_user
        reg_email    = zoomer.session.reg_email

        debug = zoomer.debug
        debug("sid "+str(zoomer.sid))
        debug(reg_user)
        #debug(zoomer.session)
        if reg_user:
            del zoomer.session.reg_user
            del zoomer.session.reg_email
            #del zoomer.session.reg_password
            webvars = zoomer.webvars
            self.verify_input(len(webvars.passwd1) >= zoomer.MIN_PASSWD_SIZE ,'Password must be at least %d characters' % zoomer.MIN_PASSWD_SIZE)
            self.verify_input(webvars.passwd1 == webvars.passwd2,'Passwords do not match')
            if self.err:
                self.msg = ('Please address the following %d items<br>' % self.err) + self.msg
                debug('Invalid input')
                return self.display_password_form()
            else:
                import dzacl
                zoomer.acl = dzacl.Acl(zoomer.db)
                reg_password = zoomer.acl.encrypt_password(webvars.passwd1)
                zoomer.acl.set_password(reg_user,reg_password,1)
                c = Wiki(include('complete.txt')).render()
                page = Page('Reset Password')
                page.content = c
                return page
        else:
            c = Wiki(include('expired.txt')).render()
            page = Page('Register')
            page.content = c
            return page


    def _reset(self):
        webvars = zoomer.webvars
        #self.verify_input(len(webvars.email) >= 5 ,'email address must be at least 5 characters')
        #self.verify_input(webvars.email.find('@') != -1 and webvars.email.find('.') != -1,'Invalid email address')
        rec = zoomer.db("select loginid,userid,email,firstname from dz_users where loginid='%s'" % (webvars.loginid))
        self.verify_input(rec,'That loginid has not been registered')
        if  self.err:
            self.msg = ('Please address the following %d items<br>' % self.err) + self.msg
            zoomer.debug('Invalid input')
            return self.display_form()

        zoomer.sid = zoomer.session.new_session(60)
        newsession = zoomer.session
        newsid = zoomer.sid
        zoomer.debug('Reset Password: '+newsid)
        import dzacl
        newsession.reg_user     = rec[0].LOGINID
        newsession.reg_email    = rec[0].EMAIL
        zoomer.acl = dzacl.Acl(zoomer.db)
        #newsession.reg_password = zoomer.acl.encrypt_password(webvars.passwd1)
        newsession.save_session(newsid,60)

        #hostaddr = zoomer.config.get('site','rooturl') + zoomer.config.get('site','mainurl')
        hostaddr = zoomer.root_url
        sitename = zoomer.config.get('site','name')
        msg = Wiki(include('email.txt') % (rec[0].FIRSTNAME,sitename,hostaddr,newsid,sitename)).render()
        import dzmail
        returnaddr = zoomer.config.get('site','register_email')
        try:
            dzmail.SendMail(returnaddr,rec[0].EMAIL,'Reset Password for '+sitename,msg)
        except:
            zoomer.log.log('D','Reset password error sending %s to %s' % (newsid,rec[0].EMAIL))
            c = Wiki(include('error.txt')).render()
            self.PAGECONTENT = c
        else:
            zoomer.log.log('D','Reset password %s sent to %s from session %s' % (newsid,rec[0].EMAIL,zoomer.sid))
            c = Wiki(include('step2.txt')).render()
            self.PAGECONTENT = c
        page = Page('Reset Password')
        page.content = self.PAGECONTENT
        return page

    def verify_input(self,valid,message):
        if not valid:
            self.msg += '* ' + message + '<br>'
            self.err += 1

    def display_password_form(self):
        self.PAGECONTENT = include('newpassfm.htm') % (self.msg,zoomer.webvars.sid)
        page = Page('Reset Password')
        page.content = self.PAGECONTENT
        return page


    def display_form(self):
        self.PAGECONTENT = include('passresetfm.htm') % (self.msg,zoomer.webvars.sid)
        page = Page('Reset Password')
        page.content = self.PAGECONTENT
        return page


result = PassReset()